﻿@using GMS.NET.Dto.System.Users.Shared
@inject GMS.NET.Web.Components.Service.Interface.System.IUsersService UserService

<Form>
    <FormItem>
        <BootstrapInput @bind-Value="@Context.User_Name" IsTrim="true" />
    </FormItem>
    <FormItem>
        <BootstrapInput @bind-Value="@Context.User_Account" IsTrim="true" />
    </FormItem>
    <FormItem>
        <AutoComplete Items="@cusids" ValueChanged="@CusIdOnChanged" 
                      PlaceHolder="@(Context.User_CusName??"请输入所属客户")"
                      Debounce="1000" OnSelectedItemChanged="CusIdOnValueChanged" DisplayText="所属客户" />
    </FormItem>
    <FormItem>
        <AutoComplete Items="@roleids" ValueChanged="@RoleIdOnChanged"
                         PlaceHolder="@(Context.User_RoleName??"请输入所属角色")"
                      Debounce="1000" OnSelectedItemChanged="RoleIdOnValueChanged" DisplayText="所属角色" />
    </FormItem>
    <FormItem>
        <Select @bind-Value="@Context.User_Type" />
    </FormItem>
    <FormItem>
        <Select @bind-Value="@Context.User_Status" />
    </FormItem>
    <FormItem>
        <BootstrapInput @bind-Value="@Context.User_Phone" IsTrim="true" />
    </FormItem>
    <FormItem>
        <BootstrapInput @bind-Value="@Context.User_Email" IsTrim="true" />
    </FormItem>
    <FormItem Cols="12">
        <Textarea @bind-Value="@Context.User_Remark" rows="3" />
    </FormItem>
</Form>

@code {
    [Parameter]
    public UsersData Context { get; set; }
    private readonly List<string> _cusids = new();
    private IEnumerable<string> cusids => _cusids;
    private List<KeyValues> FilterDatas = new();
    private async Task CusIdOnChanged(string val)
    {
        _cusids.Clear();
        FilterDatas = await UserService.GetCustsList(val);
        foreach (var item in FilterDatas)
        {
            if (!_cusids.Any(x => x == item.Key))
                _cusids.Add(item.Key!);
        }
    }
    private Task CusIdOnValueChanged(string val)
    {
        Context.User_CusId = new Guid(FilterDatas.First(x => x.Key == val).Value!.ToString()!);
        return Task.CompletedTask;
    }
    private readonly List<string> _roleids = new();
    private IEnumerable<string> roleids => _roleids;
    private List<KeyValues> FilterDatas2 = new();
    private async Task RoleIdOnChanged(string val)
    {
        _roleids.Clear();
        FilterDatas2 = await UserService.GetRolesList(val);
        foreach (var item in FilterDatas2)
        {
            if (!_roleids.Any(x => x == item.Key))
                _roleids.Add(item.Key!);
        }
    }
    private Task RoleIdOnValueChanged(string val)
    {
        Context.User_RoleId = new Guid(FilterDatas2.First(x => x.Key == val).Value!.ToString()!);
        return Task.CompletedTask;
    }
}
